<template>
  <div>
    <el-dialog :visible.sync="visible"
               :title="title"
               width="60%"
               :append-to-body="true"
               :before-close="modalClose"
               :close-on-click-modal="false"
               :close-on-press-escape="false">
      <el-form :model="submitData"
               label-position="right"
               label-width="180px"
               ref="formModelRef"
               :rules='formModelRules'>
        <el-form-item label="医院名称：" prop="hospitalId">
          <el-select v-model="submitData.hospitalId"
                     clearable
                     filterable
                     @change="handleChange()"
                     style="width: 260px"
                     placeholder="请选择医院">
            <el-option v-for="item in hospitalList"
                       :key="item.hospitalId"
                       :label="item.name"
                       :value="item.hospitalId">
            </el-option>
          </el-select>

        </el-form-item>
        <el-form-item label="医院等级：" v-if="submitData.hospitalId">
          <p>{{ submitData.level }}</p>
        </el-form-item>
        <el-form-item label="医院地址：" v-if="submitData.hospitalId">
          <p>{{ submitData.address }}</p>
        </el-form-item>
        <el-form-item label="服务价格配置：" class="is-required">
          <div v-for="(item,index) in doctorTitleList"
               style="display: flex;flex-direction: row;align-items: center;margin-bottom: 20px">
            <p>{{ item.name }}：</p>
            <el-input placeholder="请输入价格"
                      v-model="item.price"
                      style="width: 260px;margin-left: 20px"
                      clearable>
              <el-button slot="append">元</el-button>
            </el-input>
          </div>

        </el-form-item>

        <el-form-item label="处方价格配置：" class="is-required">
          <div v-for="(item,index) in doctorTitleList"
               style="display: flex;flex-direction: row;align-items: center;margin-bottom: 20px">
            <p>{{ item.name }}：</p>
            <el-input placeholder="请输入价格"
                      v-model="item.price1"
                      style="width: 260px;margin-left: 20px"
                      clearable>
              <el-button slot="append">元</el-button>
            </el-input>
          </div>

        </el-form-item>

        <el-form-item label="服务医生配置:" class="is-required">
          <el-radio-group v-model="submitData.isAllDoctor">
            <el-radio :label="1">全部医生</el-radio>
            <el-radio :label="0">自定义医生</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="submitData.isAllDoctor==0">
          <el-input placeholder="请输入该医生的平台账号"
                    v-model="doctorAccout"
                    style="width: 260px"
                    clearable>
          </el-input>
          <el-button type="primary" @click="handleAddDoctor()">增加</el-button>
        </el-form-item>
        <el-form-item v-if="submitData.serviceHospitalDoctorList.length>0&&submitData.isAllDoctor==0">
          <element-table
            :columns="columnsDoctor"
            :data="submitData.serviceHospitalDoctorList">

            <template v-slot:option="{row}">
              <el-button type="danger"
                         @click="handleDelDoctor">删除
              </el-button>
            </template>
          </element-table>

        </el-form-item>
      </el-form>
      <p align="center">
        <el-button @click="modalClose"
                   type="info">取 消
        </el-button>
        <el-button type="primary"
                   v-prevent-re-click
                   @click="handleSubmit()">确 认
        </el-button>
      </p>

    </el-dialog>
  </div>
</template>

<script>
import ElementTable from '@/base/ElementTable'

export default {
  name: "slowHospitalUpdateDialog",
  props: {
    slowHospitalData: {
      type: Object,
      default: {}
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      title: '新 增',
      loading: false,
      submitData: {
        id: '',
        companyId: '',
        hospitalId: '',
        ownerHospitalId: '',
        doctorPriceList: [],
        prescriptionDoctorPriceList: [],
        isAllDoctor: 1,
        serviceHospitalDoctorList: []
      },
      tableData: [],
      doctorAccout: '',
      columnsDoctor: [
        {
          label: '医生姓名',
          prop: 'nickName',
          align: "center",
          width: 200
        },
        {
          label: '职级',
          prop: 'doctorTitleName',
          align: "center",
          width: 200
        },
        {
          label: '所在科室',
          prop: 'departmentName',
          align: "center",
          width: 200
        }, {
          label: '操作',
          prop: 'option',
          align: 'center',
          isSlot: true,
          width: 200
        }
      ],
      hospitalList: [],
      doctorTitleList: [],
      formModelRules: {
        hospitalId: [
          {required: true, message: '请选择医院'}
        ],
      }

    }
  },
  created() {
    if (this.slowHospitalData.id) {
      // 18148941221
      this.title = '编 辑'
      this.submitData = Object.assign(this.submitData, this.slowHospitalData)
      if (this.submitData.serviceHospitalDoctorList.length > 0) {
        this.submitData.serviceHospitalDoctorList.forEach(item => {
          this.$set(item, 'nickName', item.doctorName)
        })
      }
      this.doctorTitleList = this.slowHospitalData.doctorPriceList.map(item => ({
        companyId: item.companyId,
        name: item.doctorTitleName,
        price: item.price,
        doctorTitleId: item.doctorTitleId
      }))
      this.doctorTitleList.forEach((item, index) => {
        this.slowHospitalData.prescriptionDoctorPriceList.forEach((its, idx) => {
          if (index === idx) {
            this.$set(item, 'price1', its.price)
          }

        })
      })
    } else {
      this.submitData = {
        id: '',
        companyId: '',
        hospitalId: '',
        ownerHospitalId: '',
        doctorPriceList: [],
        isAllDoctor: 1,
        serviceHospitalDoctorList: [],
        prescriptionDoctorPriceList: []
      }
      this.title = '新 增'
      this.getDoctorTitle()
    }
    if (this.slowHospitalData.organization === 'hospital') {
      this.submitData.ownerHospitalId = this.slowHospitalData.companyId
      this.submitData.companyId = ''
    } else if (this.slowHospitalData.organization === 'bclient') {
      this.submitData.companyId = this.slowHospitalData.companyId
      this.submitData.ownerHospitalId = ''
    }


    this.submitData.doctorPriceList = this.doctorTitleList
    this.submitData.prescriptionDoctorPriceList = this.doctorTitleList
    this.getHospitalList()
  },
  methods: {
    modalClose() {
      this.$emit('update:visible', false); // 直接修改父组件的属性
    },
    //查询医生
    handleAddDoctor() {
      if (this.doctorAccout) {
        let hospitalId = this.submitData.hospitalId;
        if (!hospitalId) {
          this.$message.warning('请先选择服务医院')
        } else {
          //TODO 查询医生接口
          this.getDoctorInfo(this.doctorAccout, hospitalId)
        }

      } else {
        this.$message.error('请输入医生平台账号')
      }
    },
    //删除医生
    handleDelDoctor(index) {
      this.submitData.serviceHospitalDoctorList.splice(index, 1)
    },
    getDoctorInfo(phone, hospitalIds) {
      this.post(this.api.company.getSelectDoctorInfoByPhone, {
        phone: phone,
        hospitalId: hospitalIds
      }).then((res) => {
        if (res.data.code === '200' && res.data.object.length > 0) {
          var bean = res.data.object[0]
          this.submitData.serviceHospitalDoctorList.push({
            nickName: bean.nickName,
            doctorId: bean.id,
            hospitalId: bean.hospitalId,
            loginName: bean.loginName,
            departmentName: bean.departmentName,
            doctorTitleName: bean.doctorTitleName,
            hospitalName: bean.hospitalName,
          })
          this.doctorAccout = ''
        } else {
          this.$message.warning('未查询到该医生')
        }
      })
    },
    //选择医院
    handleChange() {
      var hospitalId = this.submitData.hospitalId;
      var hospitalList = this.hospitalList;
      var filterData = hospitalList.filter(item => item.hospitalId == hospitalId)[0];
      this.submitData.level = filterData.level
      this.submitData.address = filterData.address
      this.submitData.serviceHospitalDoctorList = []
    },

    //查询医生职称
    getDoctorTitle() {
      this.post(this.api.company.getDoctorTitle, {
        pageSize: 100
      }).then((res) => {
        if (res.data.code === '200') {

          this.doctorTitleList = res.data.object
          this.doctorTitleList.forEach(item => {
            this.$set(item, 'doctorTitleId', item.id)
          })
        }
      })
    },
    //查询合作医院
    getHospitalList() {
      this.post(this.api.company.getHospitalList, {
        pageSize: 100
      }).then((res) => {
        if (res.data.code === '200') {
          this.hospitalList = res.data.object

          var hospitalId = this.submitData.hospitalId;
          if (hospitalId) {
            var hospitalList = this.hospitalList;
            var filterData = hospitalList.filter(item => item.hospitalId == hospitalId)[0];
            this.submitData.level = filterData.level
            this.submitData.address = filterData.address
          }

        }
      })
    },
    //更新
    saveServiceHospital(myData) {
      this.post(this.api.company.saveServiceHospital, myData).then((res) => {
        if (res.data.code === '200') {
          this.$message.success('更新成功')
          this.$parent.getList()
          this.modalClose()
        }
      })
    },
    //提交
    handleSubmit() {
      this.$refs.formModelRef.validate(async (valid) => {
        if (valid) {
          if (this.doctorTitleList.every(item => item.price)&&this.doctorTitleList.every(item => item.price1)) {
            this.submitData.doctorPriceList = this.doctorTitleList;
            this.submitData.prescriptionDoctorPriceList = this.doctorTitleList;
            if (this.submitData.isAllDoctor == 0 && this.submitData.serviceHospitalDoctorList.length == 0) {
              this.$message.warning('请添加平台医生')
            } else {
              let myData = JSON.parse(JSON.stringify(this.submitData))
              this.$set(myData, 'prescriptionDoctorPriceList', myData.doctorPriceList)
              myData.doctorPriceList = myData.doctorPriceList.map(item => ({
                companyId: item.companyId,
                doctorTitleId: item.doctorTitleId,
                name: item.name,
                price: item.price,
              }))
              myData.prescriptionDoctorPriceList = myData.prescriptionDoctorPriceList.map(item => ({
                companyId: item.companyId,
                doctorTitleId: item.doctorTitleId,
                name: item.name,
                price: item.price1,
              }))
              this.saveServiceHospital(myData)
            }

          } else {
            this.$message.warning('请输入医生对应价格')
          }

        }
      })

    },
  },
  components: {
    'element-table': ElementTable,
  }
}
</script>

<style scoped>

</style>
